package com.qhg.saas_server.controller;

import com.qhg.saas_server.empty.Result;
import com.qhg.saas_server.pojo.Comployee;
import com.qhg.saas_server.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 *登录模块
 */
@CrossOrigin
@RestController
public class LoginController {
    @Autowired
    private LoginService loginService;
    @PostMapping("/login")
    public Result login(@RequestBody Comployee comployee, HttpSession session) throws IOException {
//        System.out.println(comployee);
        Comployee com = loginService.login(comployee);
//        System.out.println("----------------------------"+com);
        if(com ==null || com.getComploStatus()==0){//查询不到员工信息或者员工未激活
            return Result.error("账户不存在或者账户未激活，请联系管理员进行注册或者激活账号！");
        }
        //存储员工信息，用于拦截器判断用户是否登录，未登录时无法访问所有界面
        session.setAttribute("comployee",com);
        //存储员工id，组员使用
        session.setAttribute("comploId",com.getComploId());
        return Result.success(com); //登陆成功
    }
}
